/*
 * miBus-admin.js
 */
if (!window.dataTableProcessing) {
    window.dataTableProcessing = {};
}

var createDataTable = function (selector) {
    dataTableProcessing["default"] = {
        "processing": false,
        "serverSide": false,
        "ajax": false
    };

    var asInitVals = [];
    var configObj = {
        "fnDrawCallback": function( oSettings ) {
            $('a[action=Borrar]:not([no-confirm])').on('click', function(e) {
                if (!confirm('¿Está seguro? Esta acción no se puede deshacer')) {
                    e.preventDefault();
                    return false;
                }
            });
        },
//        "bPaginate": true,
        "pagingType": "full_numbers",
//        "iDisplayLength": 10,
//        "bLengthChange": false,
//        "bFilter": true,
//        "bSort": false,
//        "bInfo": true,
        "sDom": 'Tlrt<"dt_pag_wrapper"i>p',
        "retrieve": true,
        "autoWidth": true,
//        "sScrollX": "100%",
        "oLanguage": {
            "sProcessing": "Procesando...",
            "sLengthMenu": "Mostrar _MENU_ registros",
            "sZeroRecords": "No se encontraron resultados",
            "sEmptyTable": "Ning&uacute;n dato disponible en esta tabla",
            "sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
            "sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
            "sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
            "sInfoPostFix": "",
            "sSearch": "Buscar:",
            "sUrl": "",
            "sInfoThousands": ",",
            "sLoadingRecords": "Cargando...",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "&Uacute;ltimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "fnInfoCallback": null,
            "oAria": {
                "sSortAscending": ": Activar para ordernar la columna de manera ascendente",
                "sSortDescending": ": Activar para ordernar la columna de manera descendente"
            }
        },
        "oTableTools": {
            "aButtons": [
                {
                    "sExtends": "collection",
                    "sButtonText": "Guardar como",
                    "aButtons": [
                        {
                            "sExtends": "xls",
                            "sButtonText": "Excel"
                        },
                        {
                            "sExtends": "pdf",
                            "sButtonText": "PDF"
                        }
                    ]
                }
            ],
            "sSwfPath": "files/swf/copy_csv_xls_pdf.swf"
        }
    };
    for (property in dataTableProcessing["default"]) {
        configObj["property"] = dataTableProcessing["default"]["property"];
    }
    if ($(selector).attr('id') && dataTableProcessing[$(selector).attr('id')] !== undefined) {
        for (property in dataTableProcessing[$(selector).attr('id')]) {
            configObj[property] = dataTableProcessing[$(selector).attr('id')][property];
        }
        if (configObj.ajax) {
            configObj.ajax.statusCode = {
                403: function () {
                    alert("Su sesión fue cerrada o ha caducado");
                    location.reload(true);
                }
            };
        }
    }
    var oTable = $(selector).DataTable(configObj);

    oTable.columns().eq( 0 ).each( function ( colIdx ) {
        $( 'input[type=text]', oTable.column( colIdx ).footer() ).on( 'keyup change', function () {
            oTable.column( colIdx ).search( this.value );
            if (!configObj.processing && !configObj.processing) {
                oTable.draw();
            }
        });
    });

    $("tfoot input[type=button]", selector).click({tableTarget: selector, dataTableObj: oTable}, function (eventObject) {
        /* Filter on the column (the index) of this element */
        eventObject.data.dataTableObj.draw();
    });

    $("tfoot input[type=text]", selector).each(function (i) {
        this.className = "search_init";
        asInitVals[i] = this.value;
    });

    $("tfoot input[type=text]", selector).focus(function () {
        if (this.className == "search_init")
        {
            this.className = "";
            this.value = "";
        }
    });

    $("tfoot input[type=text]", selector).blur({tableTarget: selector}, function (eventObject) {
        if (this.value == "")
        {
            this.className = "search_init";
            this.value = asInitVals[$("tfoot input[type=text]", eventObject.data.tableTarget).index(this)];
        }
    });

    $(window).bind('resize', {dataTableObj: oTable}, function (eventObject) {
        eventObject.data.dataTableObj.columns.adjust();
    });

    $(selector).on( 'draw.dt', {dataTableObj: oTable}, function ( e) {
        e.data.dataTableObj.columns.adjust();
    });

    return oTable;
};

if ($("#subcontent table.dt").length > 0) {
    createDataTable("#subcontent table.dt");
}

/*----------------------------------------------
 CLOSING BUTTTON
 ----------------------------------------------*/

$(".cerrar-negro, [class*=cerrar]").on("click tap", function () {
    $(this).parent().hide();
});

$("div[id$=menu] [class*=cerrar]").on("click tap", function (event) {
    $("#" + $(this).parent().attr("id").replace("_menu", "")).toggleClass("active");
});